package com.yy.bootdemo.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yy.bootdemo.admin.dto.LogQuery;
import com.yy.bootdemo.admin.entity.MyUser;
import com.yy.bootdemo.admin.service.MyLogService;
import com.yy.bootdemo.common.utils.PageTableRequest;
import com.yy.bootdemo.common.utils.Result;
import com.yy.bootdemo.log.aop.MyLog;
import com.yy.bootdemo.security.utils.PageUtils;
import com.yy.bootdemo.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * @author yy
 * @createTime 2020/8/8
 */
@RestController
@RequestMapping("/log")
public class LogController {
    @Autowired
    private MyLogService logService;


    @PostMapping("list")
   // @PreAuthorize("hasAnyAuthority('log:list')")
    public R logList(@RequestBody PageUtils params){
        QueryWrapper userQuery = Wrappers.query();


        Page<com.yy.bootdemo.admin.entity.MyLog> page = new Page<>(params.getCurrent(), params.getSize());

        IPage<com.yy.bootdemo.admin.entity.MyLog> userPage = logService.page(page,userQuery);



        return R.ok(userPage);  }

    @DeleteMapping("/log")
    @MyLog("删除所有INFO日志")
    @PreAuthorize("hasAnyAuthority('log:del')")
    public Result<Object> delAllByInfo(Long id){
        logService.removeById(id);
        return Result.ok().message("删除成功");
    }



    @GetMapping("/error/log")
    @PreAuthorize("hasAnyAuthority('errorLog:list')")
    public R errorLogList(PageTableRequest pageTableRequest, LogQuery logQuery){
        pageTableRequest.countOffset();
        logQuery.setLogType("ERROR");
        Page page = new Page<>(pageTableRequest.getPage(), pageTableRequest.getLimit());
        return R.ok(logService.page(page));
  }
    @DeleteMapping("/error/log")
    @MyLog("删除所有ERROR日志")
    @PreAuthorize("hasAnyAuthority('errorLog:del')")
    public Result<Object> delAllByError(Long id){
        logService.removeById(id);
        return Result.ok().message("删除成功");
    }

}
